package com.itheima.springbootwebbtalis.service.Impl;

import com.itheima.springbootwebbtalis.anno.LogOperation;
import com.itheima.springbootwebbtalis.anno.MyLog;
import com.itheima.springbootwebbtalis.commons.DeletionIsNotAllowed;
import com.itheima.springbootwebbtalis.mapper.DeptMapper;
import com.itheima.springbootwebbtalis.mapper.EmpMapper;
import com.itheima.springbootwebbtalis.pojo.Dept;
import com.itheima.springbootwebbtalis.service.DeptService;
import com.itheima.springbootwebbtalis.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> deptServiceShowAll() {
        List<Dept> depts = deptMapper.deptMapperShowAll();
        return depts;
    }

    @Override
    @MyLog
    public void deptServiceRemove(Integer id) {
      // 1.如果存在员工，则不允许删除
        Integer count =empMapper.selectByDeptId(id);
        if(count != 0){
            // 存在员工，不允许删除
            throw new DeletionIsNotAllowed("当前部门存在员工，不允许删除");
        }
        deptMapper.deptMapperRemove(id);
    }

    @Override
    @LogOperation
    public void deptServiceAdd(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.deptMapperAdd(dept);
    }

    @Override
    public Dept deptServiceShowId(Integer id) {
        return deptMapper.deptMapperShowById(id);

    }

    @Override
    @LogOperation
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
     deptMapper.update(dept);
    }
}
